﻿Imports AutoCAD

''' <summary>
''' 绘制轮廓线
''' </summary>
Public Class CadDrawOutlineServiceImpl
    Private ms As IAcadBlock
    Private evalService As IEvalService = New EvalServiceImpl

    Public Function draw(ByRef coordinates As List(Of Double), ByRef bulges As List(Of Double), ByVal closed As Boolean) As AcadLWPolyline
        Dim pl As AcadLWPolyline = ms.AddLightWeightPolyline(coordinates.ToArray)
        With pl
            .Closed = closed
            For i = 0 To bulges.Count - 1
                pl.SetBulge(i, bulges(i))
            Next i
        End With

        Return pl
    End Function


    Public Function draw(ByRef outlineLineTable As DataTable, ByVal closed As Boolean) As AcadLWPolyline

        Dim coordinates As New List(Of Double)
        Dim bulges As New List(Of Double)
        For Each dr As DataRow In outlineLineTable.Rows
            coordinates.Add(dr("x"))
            coordinates.Add(dr("y"))
            bulges.Add(dr("bulge"))
        Next dr

        Return draw(coordinates, bulges, closed)
    End Function


    Public Function getDrawData(ByRef outlineLineTable As DataTable, ByRef [property] As Dictionary(Of String, Object)) As DataTable
        Dim data As DataTable = outlineLineTable '轮廓数据行
        Dim result As DataTable = buildDataTable() '声明轮廓数据表

        Dim newDataRow As DataRow
        For Each dr As DataRow In data.Rows '遍历行数据
            newDataRow = result.NewRow
            For Each dc As DataColumn In data.Columns '遍历每一个属性
                Dim colName As String = dc.ColumnName
                newDataRow(colName) = evalService.AnalyzeExpressions_ByDict(dr(colName), [property]) '计算每个孔属性的表达式
            Next dc
            result.Rows.Add(newDataRow)
        Next dr
        Return result
    End Function

    Public Function buildDataTable() As DataTable
        Dim dt As New DataTable
        With dt.Columns
            .Add("outline_line_id", GetType(Integer))
            .Add("outline_head_id", GetType(Integer))
            .Add("index", GetType(Integer))
            .Add("x", GetType(Double))
            .Add("y", GetType(Double))
            .Add("bulge", GetType(Double))
        End With
        Return dt
    End Function



#Region "TEST"

    Public Function test2() As DataTable

        ' 获得远程数据
        Dim mysqlService As IMySqlService = New MySqlServiceImpl
        Dim outlineHeadId As Integer = 3
        mysqlService.connection()
        Dim sql As String
        sql = $"select * from outline_line where outline_head_id = {outlineHeadId} order by `index` asc"
        Dim lineTable As DataTable = mysqlService.select(sql)
        sql = $"select * from outline_property where outline_head_id = {outlineHeadId}"
        Dim propertyTable As DataTable = mysqlService.select(sql)
        Dim dict As New Dictionary(Of String, Object)
        For Each dr As DataRow In propertyTable.Rows
            dict.Add(dr("name"), dr("expression"))
        Next

        ' 获取绘图数据
        Dim data As DataTable = getDrawData(lineTable, dict)

        ' 执行绘图
        Dim cadApplicationService As ICadApplicationService = New CadApplicationServiceImpl
        Dim doc As AcadDocument = cadApplicationService.GetActiveDocument
        Dim cadDrawHole As New CadDrawHole2ServiceImpl
        Me.ms = doc.ModelSpace
        draw(data, True) ' 绘制图形

        Return data

    End Function

#End Region

End Class
